package com.jz.yanshe.ui;

import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.MotionEvent;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.jz.jd.rest.business.RestProxy;
import com.jz.jd.rest.response.ConfigResponse;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.jz.yanshe.eventbus.FragmentTabEvent;
import com.jz.yanshe.ui.fragment.MallFragment;
import com.jz.yanshe.ui.fragment.MineFragment;
import com.jz.yanshe.ui.fragment.ShoppingBagFragment;
import com.jz.yanshe.ui.fragment.VipFragment;
import com.jz.yanshe.util.PreferencesHelper;
import com.jz.yanshe.base.BaseActivity;
import jz.com.feierman.R;


/**
 * Created by Administrator on 2018/2/26.
 */

public class HomeActivity extends BaseActivity implements BottomNavigationBar.OnTabSelectedListener {

    //    @BindView(R.id.bottom_navigation_bar)
    BottomNavigationBar bottomNavigationBar;

//    @BindView(R.id.btn_kefu)
//    TextView btn_kefu;

    private ArrayList<Fragment> fragments;
    private Handler myHandler = new Handler();

    int payment_credential_state = -100;



    @Override
    protected void initViews() {
        bottomNavigationBar = findViewById(R.id.bottom_navigation_bar);
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC
                );
        bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.icon_bottom_mall1, "商城").setActiveColor("#e52e2e"))
                .addItem(new BottomNavigationItem(R.mipmap.iocn_bottom_buycar1, "购物袋").setActiveColor("#e52e2e"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_bottom_vipcenter1, "会员中心").setActiveColor("#e52e2e"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_bottom_mine, "我的").setActiveColor("#e52e2e"))
                .setFirstSelectedPosition(0)
                .initialise();

        fragments = getFragments();
        setDefaultFragment();
        bottomNavigationBar.setTabSelectedListener(this);
    }

    @Override
    protected void initData() {
        getConfigList();
    }


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_home;
    }


    /**
     * 设置默认的
     */
    private void setDefaultFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.layFrame, MallFragment.newInstance());
        transaction.commit();
    }


    private ArrayList<Fragment> getFragments() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(MallFragment.newInstance());
        fragments.add(ShoppingBagFragment.newInstance());
        fragments.add(VipFragment.newInstance());
        fragments.add(MineFragment.newInstance());
        return fragments;
    }


    @Override
    public void onTabSelected(int position) {
        if (position != 0) {
            if (PreferencesHelper.getInstance().getString(this, "token") == null || PreferencesHelper.getInstance().getString(this, "token").equals("")) {
                toLogin(2);
                myHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        bottomNavigationBar.selectTab(0);
                    }
                }, 200);
                return;
            }
        }


//            if (position == 2) {
//                if (PreferencesHelper.getInstance().getString(this, "user_type") == null || PreferencesHelper.getInstance().getString(this, "user_type").equals("") ) {
//                    toLogin(2);
//                    return;
//                }
//                if(PreferencesHelper.getInstance().getString(this, "user_type").equals("1")){
//
//                }
//            }
//        }


        if (fragments != null) {
            if (position < fragments.size()) {
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                Fragment fragment = fragments.get(position);
                if (fragment.isAdded()) {
                    ft.replace(R.id.layFrame, fragment);
                } else {
                    ft.add(R.id.layFrame, fragment);
                }
                ft.commitAllowingStateLoss();
            }
        }

//        if (position != 0) {
//            if (PreferencesHelper.getInstance().getString(this, "token") == null || PreferencesHelper.getInstance().getString(this, "token").equals("")) {
//                onTabSelected(0);
//            }
//            if (position == 2) {
//                if (PreferencesHelper.getInstance().getString(this, "user_type") == null || PreferencesHelper.getInstance().getString(this, "user_type").equals("") || PreferencesHelper.getInstance().getString(this, "user_type").equals("1")) {
//                    onTabSelected(0);
//                }
//            }
//        }

    }

    @Override
    public void onTabUnselected(int position) {


        if (fragments != null) {
            if (position < fragments.size()) {
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                Fragment fragment = fragments.get(position);
                ft.remove(fragment);
                ft.commitAllowingStateLoss();
            }
        }


//        if (position == 0) {
//            if (PreferencesHelper.getInstance().getString(this, "token") == null || PreferencesHelper.getInstance().getString(this, "token").equals("")) {
////                toLogin(2);
//                bottomNavigationBar.getselectTab(0);
//            }
//            if (position == 2) {
//                if (PreferencesHelper.getInstance().getString(this, "user_type") == null || PreferencesHelper.getInstance().getString(this, "user_type").equals("") || PreferencesHelper.getInstance().getString(this, "user_type").equals("1")) {
////                    toLogin(2);
//                }
//            }
////            EventBus.getDefault().post(new FragmentTabEvent("1"));
//        }
    }

    @Override
    public void onTabReselected(int position) {

    }



    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(FragmentTabEvent event) {
        if (event.message.equals("1")) {
            bottomNavigationBar.selectTab(0);
        }
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    private void getConfigList(){
        RestProxy.getInstance().getConfigList(PreferencesHelper.getInstance().getString(this, "token"), "14", new Observer<ConfigResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ConfigResponse configResponse) {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
